// Copyright (C) ZHENG Zhong <heavyzheng nospam-at gmail D0T com>
//
// $Id: logger.cpp 216 2008-09-03 08:13:29Z heavyzheng $
//

#ifndef ZZHENG_LOGGING_SOURCE
#define ZZHENG_LOGGING_SOURCE
#endif // ZZHENG_LOGGING_SOURCE

#include <zzheng/logging/none/logger.hpp>
#include <zzheng/logging/log_level.hpp>
#include <zzheng/logging/common.hpp>

#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <cassert>
#include <string>

namespace zzheng {
namespace logging {
namespace none_ {

    logger::logger(const std::string& name, const log_level& threshold)
    : name_(name), threshold_(threshold) {
        // Do nothing.
    }

    std::string logger::name() const {
        return name_;
    }

    log_level logger::threshold() const {
        return threshold_;
    }

    void logger::set_threshold(const log_level& threshold) {
        threshold_ = threshold;
    }

    boost::weak_ptr<logger> logger::child(const std::string& child_name) {
        std::string legal_name = remove_illegal_chars_in_name(child_name);
        assert(!legal_name.empty() && "Child logger name should not be empty.");
        child_map::iterator i = children_.find(legal_name);
        if (i != children_.end()) {
            // The requested child logger already exists: return it.
            return boost::weak_ptr<logger>(i->second);
        } else {
            // The requested child logger does not exist: create a new one and return.
            std::string full_name = (name_.empty() ? legal_name : name_ + "." + legal_name);
            boost::shared_ptr<logger> child(new logger(full_name, threshold_));
            children_.insert(std::make_pair(legal_name, child));
            return boost::weak_ptr<logger>(child);
        }
    }

    void logger::log(const log_level&, const std::string&) {
        // The none logger does nothing.
    }

} // namespace zzheng::logging::none_
} // namespace zzheng::logging
} // namespace zzheng



